﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data.SqlClient;

public partial class login : System.Web.UI.Page
{
    private static string sqlconn = System.Configuration.ConfigurationManager.AppSettings["sqlcon"].ToString().Trim();
    protected void Page_Load(object sender, EventArgs e)
    {

    }
    protected void btnLend_Click(object sender, EventArgs e)
    {
        string id = txtName.Text.Trim();
        string ps = txtPASSWORD.Text.Trim();
        SqlConnection conn = new SqlConnection(sqlconn);
        try
        {
            conn.Open();
            string sql = "select count(*) id from Register where U_id='" + id + "'and U_ps='" + ps + "'";


            SqlCommand comm = new SqlCommand(sql, conn);
            int result = (int)comm.ExecuteScalar();

            if (result == 1)
            {

                Session["id"] = id;
                Response.Redirect("~/Default.aspx");
                //Response.Write("<script>window.alert(\"登录成功！\");location.href='About.aspx';</script>");
            }
            else
            {
                Response.Write("<script>window.alert(\"用户不存在！\")</script>");
            }

        }
        catch (SqlException ex)
        { throw new Exception(ex.Message); }
        finally
        {
            conn.Close();
        }

    }
    protected void btnLend0_Click(object sender, EventArgs e)
    {
        string id = txtName.Text.Trim();
        string ps = txtPASSWORD.Text.Trim();

        SqlConnection conn = new SqlConnection(sqlconn);
        try
        {

            conn.Open();

            string sql = "insert into Register(U_id,U_ps) values('" + id + "','" + ps + "')";
            SqlCommand comm = new SqlCommand(sql, conn);
            comm.ExecuteNonQuery();
            Session["id"] = id;
            Response.Redirect("~/用户.aspx");
        }
        catch (SqlException ex)
        {
            throw new Exception(ex.Message);

        }
        finally
        {
            conn.Close();
        }
    }
    protected void btn1_Click(object sender, EventArgs e)
    {
        string id = txtName.Text.Trim();
        string sql = "select count(*) from Register where U_id='" + id + "'";
        SqlConnection conn = new SqlConnection(sqlconn);
        try
        {
            conn.Open();
            SqlCommand comm = new SqlCommand(sql, conn);
            int result = (int)comm.ExecuteScalar();
            if (result > 0)
            {
                lblMsg.Visible = true;
                lblMsg.Text = "此账号不可用！";
            }
            else
            {
                lblMsg.Visible = true;
                lblMsg.Text = "此账号可用！";
            }

        }
        catch
        {

        }
    }
    protected void btnLend1_Click(object sender, EventArgs e)
    {
        Session.RemoveAll();
        Response.Write("<script>alert('欢迎光临 ');window.location.href='首页.aspx';</script>");
    }
}